package com.slot.faraohgoldview;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;

public class FaraohGoldView extends Activity implements View.OnClickListener {
	/** Called when the activity is first created. */
	static final int REELS = 5;
	static final int REELS_LENGTH = 18;// { 18, 20, 22, 24, 26 };
	static final int[] SHIFT_REELS_Y = { 41, 156, 271, 384, 499 };
	static final int[][] STRIP_REGULAR = {
			{ 5,10,0,8,11,7,10,6,1,10,5,7,8,3,6,8,12,9,8,2,6,4,10,8,9,10,8,9,3,6 },
			{4,7,0,6,7,10,1,8,9,11,5,10,6,2,9,8,1,7,9,4,7,10,12,11,3,9,4,7,5,2,9,11,3,7,9,5,7,9,4,11,3,8,6},
			{ 10,8,0,10,3,7,8,1,7,5,9,3,10,11,1,9,7,8,11,12,8,10,4,10,2,9,8,3,6,4,8,10,1,6,5,11,6,2,10,8,5,6,2},
			{ 7,10,0,8,2,6,9,3,8,1,6,5,9,4,11,9,5,11,7,4,10,12,7,5,9,1,6,5,11,4,7,10,0,8,2,5,11,3,9,4,11,9,2 },
			{ 7,6,0,7,3,8,4,11,9,5,10,2,6,7,1,8,4,9,10,1,11,12,4,9,3,11,5,6,1,9,7,1,11,9,2,7,9,3,11,4,7,11,1 } };
	static final int[] LINES = { 1, 3, 5, 7, 9 };
	static final int[] SCATTER_TABLE = { 0, 0, 2, 5, 20, 500 };
	static final int[] NINE_TABLE = { 0, 0, 2, 5, 25, 100 };
	static final int[] TENJACKQUEEN_TABLE = { 0, 0, 0, 5, 25, 100 };
	static final int[] KINGACE_TABLE = { 0, 0, 0, 10, 50, 125 };
	static final int[] BUGCAT_TABLE = { 0, 0, 0, 15, 75, 250 };
	static final int[] BIRD_TABLE = { 0, 0, 0, 20, 100, 400 };
	static final int[] SPHINXTABLET_TABLE = { 0, 0, 2, 25, 125, 750 };
	static final int[] PHARAOH_TABLE = { 0, 0, 10, 250, 2500, 9000 };

	static final int DISABLE = 0;
	static final int SPIN_READY = 1;
	static final int REELS_ANIM = 2;
	static final int SHOW_WIN = 3;
	static final int WIN_WAIT = 4;
	static final int DOUBLE = 5;
	static final int FG_READY = 6;
	static final int INFO = 7;

	private BtmPanel bpanel;
	private Reel[] reel = new Reel[REELS];
	private TopPanel tpanel;
	private DoublePanel dpanel;
	private InfoPanel ipanel;
	private Animation anim, dpanim, ianim;
	private Button btnStart, btnAuto, btnLines, btnInfo, btnBet;
	private TextView winLineField, creditField, betField, linesField,
			betlinesField, winField, infoField;
	private RelativeLayout layoutBP;
	private RelativeLayout.LayoutParams layoutParams, dplayoutParams,
			iplayoutParams, layoutParamsInfoField;
	private boolean isAnim = false;
	private boolean isBpUp = false;
	private boolean isSpinAfter = false;
	private boolean isAuto = false;
	private boolean isFGcountup = false;
	private boolean isFG = false;
	private boolean isFirstTap = true;
	private int Mode, win, showline, fgwin, infoFieldTextSize1Str, infoFieldTextSize2Str,fieldTextSize;
	private int fggames = 0;
	private int lines_index = 0;
	private int[] bet = { 1, 2, 3, 4, 5, 10, 15, 20, 30, 40, 50, 100 };
	private int bet_index = 0;
	private int credit = 100;
	private int fgcount = 0;
	private int[][] symbols = new int[5][3];
	private int[] showLines, showFigs, winPerLine;
	static int maxDim;
	private float tempY = 0;
	private float scaleX, scaleY;
	private Handler reelStopHandler = new Handler();
	private MediaPlayer mp_spinloop, mp_winwait, mp_card, mp_fgloop,mp_linebet;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		scaleY = (float) displaymetrics.heightPixels / 480;
		scaleX = (float) displaymetrics.widthPixels / 640;
		maxDim=Math.max(displaymetrics.heightPixels, displaymetrics.widthPixels);
		if(displaymetrics.heightPixels==480){
			infoFieldTextSize2Str=10;
			infoFieldTextSize1Str=14;
			fieldTextSize=16;
		} else {			
			infoFieldTextSize2Str=15;
			infoFieldTextSize1Str=20;
			fieldTextSize=22;
		}

		layoutParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		dplayoutParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		iplayoutParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		layoutParamsInfoField = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		RelativeLayout.LayoutParams layoutParamsWinLineField = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		RelativeLayout.LayoutParams layoutParamsBtnStart = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		RelativeLayout.LayoutParams layoutParamsBtnAuto = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		RelativeLayout.LayoutParams layoutParamsBtnBet = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		RelativeLayout.LayoutParams layoutParamsBtnLines = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		RelativeLayout.LayoutParams layoutParamsBtnInfo = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		RelativeLayout.LayoutParams layoutParamsCreditField = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		RelativeLayout.LayoutParams layoutParamsBetField = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		RelativeLayout.LayoutParams layoutParamsLinesField = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		RelativeLayout.LayoutParams layoutParamsBetLinesField = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		RelativeLayout.LayoutParams layoutParamsWinField = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		RelativeLayout layout = new RelativeLayout(this);
		layout.setBackgroundColor(Color.WHITE);
		layoutBP = new RelativeLayout(this);

		layoutParams.setMargins(0, (int) (380 * scaleY), 0,
				(int) (-380 * scaleY));
		dplayoutParams.setMargins((int) (640 * scaleX), 0,
				(int) (-640 * scaleX), 0);
		iplayoutParams.setMargins(0, (int) (-480 * scaleY), 0,
				(int) (480 * scaleY));
		layoutParamsBtnStart.setMargins((int) (505 * scaleX),
				(int) (110 * scaleY), (int) (-505 * scaleX),
				(int) (-110 * scaleY));
		
		tpanel = new TopPanel(this,scaleX,scaleY);
		for (int i = 0; i < REELS; i++) {
			reel[i] = new Reel(this, SHIFT_REELS_Y[i], STRIP_REGULAR[i],
					REELS_LENGTH + i * 2);
			layout.addView(reel[i]);
		}		
		winLineField = new TextView(this);
		winLineField.setText("");
		winLineField.setTextColor(Color.YELLOW);
		winLineField.setTextSize(infoFieldTextSize2Str * scaleY);
		layoutParamsWinLineField
				.setMargins((int) (20 * scaleX), (int) (15 * scaleY),
						(int) (-20 * scaleX), (int) (-15 * scaleY));
		winLineField.setLayoutParams(layoutParamsWinLineField);
		bpanel = new BtmPanel(this);
		dpanel = new DoublePanel(this);
		ipanel = new InfoPanel(this);
		layout.addView(tpanel);
		layout.addView(winLineField);
		layoutBP.addView(bpanel);

		btnStart = new Button(this);
		btnStart.setText("START");
		btnStart.setTextSize(12 * scaleX);
		btnStart.setTypeface(null, Typeface.BOLD);
		btnStart.setHeight((int) (100 * scaleY));
		btnStart.setWidth((int) (120 * scaleX));
		btnStart.setOnClickListener(this);
		btnStart.setLayoutParams(layoutParamsBtnStart);
		layoutBP.addView(btnStart);

		btnAuto = new Button(this);
		btnAuto.setText("AUTO");
		btnAuto.setTextSize(12 * scaleX);
		btnAuto.setTypeface(null, Typeface.BOLD);
		btnAuto.setHeight((int) (100 * scaleY));
		btnAuto.setWidth((int) (100 * scaleX));
		btnAuto.setOnClickListener(this);
		layoutParamsBtnAuto.setMargins((int) (400 * scaleX),
				(int) (110 * scaleY), (int) (-400 * scaleX),
				(int) (-110 * scaleY));
		btnAuto.setLayoutParams(layoutParamsBtnAuto);
		layoutBP.addView(btnAuto);

		btnBet = new Button(this);
		btnBet.setText("BET");
		btnBet.setTextSize(12 * scaleX);
		btnBet.setTypeface(null, Typeface.BOLD);
		btnBet.setHeight((int) (100 * scaleY));
		btnBet.setWidth((int) (100 * scaleX));
		btnBet.setOnClickListener(this);
		layoutParamsBtnBet.setMargins((int) (225 * scaleX),
				(int) (110 * scaleY), (int) (-225 * scaleX),
				(int) (-110 * scaleY));
		btnBet.setLayoutParams(layoutParamsBtnBet);
		layoutBP.addView(btnBet);

		btnLines = new Button(this);
		btnLines.setText("LINES");
		btnLines.setTextSize(12 * scaleX);
		btnLines.setTypeface(null, Typeface.BOLD);
		btnLines.setHeight((int) (100 * scaleY));
		btnLines.setWidth((int) (100 * scaleX));
		btnLines.setOnClickListener(this);
		layoutParamsBtnLines.setMargins((int) (120 * scaleX),
				(int) (110 * scaleY), (int) (-120 * scaleX),
				(int) (-110 * scaleY));
		btnLines.setLayoutParams(layoutParamsBtnLines);
		layoutBP.addView(btnLines);

		btnInfo = new Button(this);
		btnInfo.setText("INFO");
		btnInfo.setTextSize(12 * scaleX);
		btnInfo.setTypeface(null, Typeface.BOLD);
		btnInfo.setHeight((int) (100 * scaleY));
		btnInfo.setWidth((int) (100 * scaleX));
		btnInfo.setOnClickListener(this);
		layoutParamsBtnInfo.setMargins((int) (15 * scaleX),
				(int) (110 * scaleY), (int) (-15 * scaleX),
				(int) (-110 * scaleY));
		btnInfo.setLayoutParams(layoutParamsBtnInfo);
		layoutBP.addView(btnInfo);

		creditField = new TextView(this);
		creditField.setText("" + credit);
		creditField.setTextColor(Color.YELLOW);
		creditField.setTextSize(fieldTextSize * scaleY);
		layoutParamsCreditField.setMargins((int) (100 * scaleX),
				(int) (63 * scaleY), (int) (-100 * scaleX),
				(int) (-63 * scaleY));
		creditField.setLayoutParams(layoutParamsCreditField);
		layoutBP.addView(creditField);

		betField = new TextView(this);
		betField.setText("" + bet[bet_index] * LINES[lines_index]);
		betField.setTextColor(Color.YELLOW);
		betField.setTextSize(fieldTextSize * scaleY);
		layoutParamsBetField.setMargins((int) (590 * scaleX),
				(int) (63 * scaleY), (int) (-590 * scaleX),
				(int) (-63 * scaleY));
		betField.setLayoutParams(layoutParamsBetField);
		layoutBP.addView(betField);

		linesField = new TextView(this);
		linesField.setText("" + LINES[lines_index]);
		linesField.setTextColor(Color.CYAN);
		linesField.setTextSize(fieldTextSize * scaleY);
		layoutParamsLinesField.setMargins((int) (595 * scaleX),
				(int) (30 * scaleY), (int) (-595 * scaleX),
				(int) (-30 * scaleY));
		linesField.setLayoutParams(layoutParamsLinesField);
		layoutBP.addView(linesField);

		betlinesField = new TextView(this);
		betlinesField.setText("" + bet[bet_index]);
		betlinesField.setTextColor(Color.YELLOW);
		betlinesField.setTextSize(fieldTextSize * scaleY);
		layoutParamsBetLinesField.setMargins((int) (595 * scaleX),
				(int) (10 * scaleY), (int) (-595 * scaleX), (int) (-10 * scaleY));
		betlinesField.setLayoutParams(layoutParamsBetLinesField);
		layoutBP.addView(betlinesField);

		winField = new TextView(this);
		winField.setText("");
		winField.setTextColor(Color.WHITE);
		winField.setTextSize(infoFieldTextSize1Str * scaleY);
		layoutParamsWinField.setMargins((int) (400 * scaleX),
				(int) (55 * scaleY), (int) (-400 * scaleX),
				(int) (-55 * scaleY));
		winField.setLayoutParams(layoutParamsWinField);
		layoutBP.addView(winField);

		infoField = new TextView(this);
		infoField.setText("       SLIDE THIS PANEL UP TO MAKE BET\n"
				+ "              OR TAP SCREEN TO PLAY");
		infoField.setTextColor(Color.WHITE);
		infoField.setTextSize(infoFieldTextSize2Str * scaleY);
		layoutParamsInfoField
				.setMargins((int) (60 * scaleX), (int) (12 * scaleY),
						(int) (-60 * scaleX), (int) (-12 * scaleY));
		infoField.setLayoutParams(layoutParamsInfoField);
		layoutBP.addView(infoField);

		layout.addView(layoutBP);
		layout.addView(dpanel);
		layout.addView(ipanel);

		setContentView(layout);
		layoutBP.setLayoutParams(layoutParams);
		dpanel.setLayoutParams(dplayoutParams);
		ipanel.setLayoutParams(iplayoutParams);

		mp_fgloop = MediaPlayer.create(this, R.raw.fg);
		mp_fgloop.setLooping(true);
		mp_card = MediaPlayer.create(this, R.raw.card);
		mp_winwait = MediaPlayer.create(getBaseContext(), R.raw.winwait);
		mp_linebet= MediaPlayer.create(getBaseContext(), R.raw.chlinebet);

		Mode = SPIN_READY;
		

	}

	@Override
	public void onClick(View v) {
		if (!isBpUp)
			return;
		winField.setText("");
		if (btnStart.equals(v)) {
			if (Mode == SPIN_READY
					&& credit >= bet[bet_index] * LINES[lines_index]) {
				isSpinAfter = true;
				AnimBpDown(layoutBP);
			} else if (Mode == WIN_WAIT || Mode == DOUBLE)
				takeWin();
		} else if (btnLines.equals(v)) {
			if (Mode == SPIN_READY) {
				if (lines_index < LINES.length - 1)
					lines_index++;
				else
					lines_index = 0;
				int[] LineFigNow = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
				for (int i = 0; i < LINES[lines_index]; i++) {
					LineFigNow[i] = 2;
				}
				tpanel.setLines(LineFigNow);
				ipanel.setTable(bet[bet_index],LINES[lines_index]);
				linesField.setText("" + LINES[lines_index]);
				betField.setText("" + bet[bet_index] * LINES[lines_index]);
				mp_linebet.start();
			} else if (Mode == DOUBLE && !isAnim) {
				mp_card.stop();
				if (Math.random() < 0.5f) {
					dpanel.setScreen(1, 0, 2);// red wins
					win *= 2;
					MediaPlayer mp = MediaPlayer.create(getBaseContext(),
							R.raw.dblewin);
					mp.start();
				} else {
					dpanel.setScreen(1, 0, 1);
					win = 0;
				}
				reelStopHandler.postDelayed(ResetDouble, 1000);
				Mode = DISABLE;
			}
		} else if (btnBet.equals(v)) {
			if (Mode == SPIN_READY) {
				if (bet_index < 11)
					bet_index++;
				else
					bet_index = 0;
				tpanel.setBet(bet[bet_index]);
				ipanel.setTable(bet[bet_index],LINES[lines_index]);
				betField.setText("" + bet[bet_index] * LINES[lines_index]);
				betlinesField.setText("" + bet[bet_index]);
				mp_linebet.start();
			} else if (Mode == DOUBLE && !isAnim) {
				mp_card.stop();
				if (Math.random() < 0.5f) {
					dpanel.setScreen(0, 1, 1);// black wins
					win *= 2;
					MediaPlayer mp = MediaPlayer.create(getBaseContext(),
							R.raw.dblewin);
					mp.start();
				} else {
					dpanel.setScreen(0, 1, 2);
					win = 0;
				}
				reelStopHandler.postDelayed(ResetDouble, 1000);
				Mode = DISABLE;
			}
		} else if (btnAuto.equals(v)) {
			if (Mode == DOUBLE)
				return;
			if (isAuto) {
				isAuto = false;
				AnimBpDown(layoutBP);
				tpanel.setAuto(false);
				MediaPlayer mp = MediaPlayer.create(getBaseContext(),
						R.raw.auto);
				mp.start();
			} else if (Mode == SPIN_READY
					&& credit >= bet[bet_index] * LINES[lines_index]) {
				isAuto = true;
				isSpinAfter = true;
				AnimBpDown(layoutBP);
				tpanel.setAuto(true);
				MediaPlayer mp = MediaPlayer.create(getBaseContext(),
						R.raw.auto);
				mp.start();
			} else if (Mode == WIN_WAIT) {
				AnimDPanel(dpanel);
				dpanel.setWon(win);
				reelStopHandler.removeCallbacks(ShowWins);
				Mode = DOUBLE;
			}
		} else if (btnInfo.equals(v)) {
			if (Mode == SPIN_READY) {
				AnimInfPanel(ipanel);
				Mode = INFO;
				MediaPlayer mp = MediaPlayer.create(getBaseContext(),
						R.raw.info);
				mp.start();
			}
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (isAnim || Mode == DOUBLE){
			return true;
		}
		if(isFirstTap){
			isFirstTap=false;
			AnimBpUp(layoutBP);
			return true;
		}
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (event.getY() < 380 * bpanel.scaleY) {
				if (Mode == FG_READY) {
					isFG = true;
					tpanel.setFGtablet(0);
					mp_fgloop = MediaPlayer.create(this, R.raw.fg);
					mp_fgloop.setLooping(true);
					mp_fgloop.start();
					Mode = SPIN_READY;
					fgcount++;
					startReels();
				} else if (Mode == INFO) {
					iplayoutParams.setMargins(0, (int) (-480 * scaleY), 0,
							(int) (480 * scaleY));
					ipanel.setLayoutParams(iplayoutParams);
					Mode = SPIN_READY;
				} else {
					
					if (!isAuto) {
						if (Mode == SPIN_READY
								&& !isBpUp
								&& credit >= bet[bet_index]
										* LINES[lines_index]) {
							winField.setText("");
							startReels();
						} else if (Mode == WIN_WAIT){
							takeWin();
						}
							
					} else {
						isAuto = false;
						tpanel.setAuto(false);
						MediaPlayer mp = MediaPlayer.create(getBaseContext(),
								R.raw.auto);
						mp.start();
					}
				}
			}
			tempY = event.getY();
		} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
			if ((Mode == SPIN_READY || Mode == WIN_WAIT) && !isBpUp
					&& event.getY() > 380 * bpanel.scaleY
					&& event.getY() < tempY) {
				AnimBpUp(layoutBP);
				if (Mode == WIN_WAIT) {
					btnStart.setText("TAKE WIN");
					btnAuto.setText("DOUBLE");
					btnBet.setText("BLACK");
					btnLines.setText("RED");
				}
			} else if ((Mode == SPIN_READY || Mode == WIN_WAIT) && isBpUp
					&& event.getY() < 350 * bpanel.scaleY
					&& event.getY() > tempY) {
				AnimBpDown(layoutBP);
			}
		}
		return true;
	}

	private void AnimBpUp(View view) {
		bpanel.setAlpha(180);
		anim = new TranslateAnimation(0, 0, 0, -110 * bpanel.scaleY);
		anim.setDuration(300L);
		anim.setFillAfter(true);
		anim.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationRepeat(Animation arg0) {

			}

			@Override
			public void onAnimationStart(Animation animation) {
				isAnim = true;
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				layoutBP.clearAnimation();
				layoutParams.setMargins(0, (int) (270 * bpanel.scaleY), 0,
						(int) (-270 * bpanel.scaleY));
				layoutBP.setLayoutParams(layoutParams);
				isAnim = false;
				isBpUp = true;
			}

		});
		view.startAnimation(anim);
	}

	private void AnimBpDown(View view) {

		anim = new TranslateAnimation(0, 0, 0, 110 * bpanel.scaleY);
		anim.setDuration(300L);
		anim.setFillAfter(true);
		anim.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationRepeat(Animation arg0) {

			}

			@Override
			public void onAnimationStart(Animation animation) {
				isAnim = true;
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				layoutBP.clearAnimation();
				layoutParams.setMargins(0, (int) (380 * bpanel.scaleY), 0,
						(int) (-380 * bpanel.scaleY));
				layoutBP.setLayoutParams(layoutParams);
				isAnim = false;
				isBpUp = false;
				bpanel.setAlpha(255);
				if (isSpinAfter) {
					isSpinAfter = false;
					if (Mode == SPIN_READY || Mode == WIN_WAIT)
						startReels();
				}
			}

		});
		view.startAnimation(anim);
	}

	private void AnimDPanel(View view) {
		dpanim = new TranslateAnimation(0, -640 * scaleX, 0, 0);
		dpanim.setDuration(700L);
		dpanim.setFillAfter(true);
		dpanim.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationRepeat(Animation arg0) {

			}

			@Override
			public void onAnimationStart(Animation animation) {
				isAnim = true;
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				dpanel.clearAnimation();
				dplayoutParams.setMargins(0, 0, 0, 0);
				dpanel.setLayoutParams(dplayoutParams);
				tpanel.setFGtablet(0);
				mp_winwait.stop();
				mp_card = MediaPlayer.create(getBaseContext(), R.raw.card);
				mp_card.setLooping(true);
				mp_card.start();
				isAnim = false;
			}

		});
		view.startAnimation(dpanim);
	}

	private void AnimInfPanel(View view) {
		ianim = new TranslateAnimation(0, 0, 0, 480 * scaleY);
		ianim.setDuration(700L);
		ianim.setFillAfter(true);
		ianim.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationRepeat(Animation arg0) {

			}

			@Override
			public void onAnimationStart(Animation animation) {
				isAnim = true;
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				ipanel.clearAnimation();
				iplayoutParams.setMargins(0, 0, 0, 0);
				ipanel.setLayoutParams(iplayoutParams);
				isAnim = false;
			}

		});
		view.startAnimation(ianim);
	}

	private boolean isAnimReels() {
		boolean isAnimReel = false;
		for (int i = 0; i < REELS; i++) {
			if (reel[i].isAnimReel)
				isAnimReel = true;
		}
		return isAnimReel;
	}

	private void startReels() {
		if (isFG
				|| ((Mode == SPIN_READY || Mode == WIN_WAIT) && !isBpUp && credit >= bet[bet_index]
						* LINES[lines_index])) {
			for (int i = 0; i < REELS; i++) {
				reel[i].startReel();
			}
			int[] LineFigNow = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
			for (int i = 0; i < LINES[lines_index]; i++) {
				LineFigNow[i] = 1;
			}
			tpanel.setLines(LineFigNow);
			reelStopHandler.postDelayed(StopReels, 2000);
			if (!isFG) {
				credit -= bet[bet_index] * LINES[lines_index];
				mp_spinloop = MediaPlayer.create(getBaseContext(),
						R.raw.reelloop);
				mp_spinloop.setLooping(true);
				mp_spinloop.seekTo((int) (Math.random()*5000));
				mp_spinloop.start();
			}
			creditField.setText("" + credit);
			if (isFG) {
				infoField.setText("                     FREE GAME  " + fgcount
						+ "  OF  " + fggames);
				infoField.setTextSize(infoFieldTextSize1Str * scaleY);
				layoutParamsInfoField.setMargins((int) (10 * scaleX),
						(int) (20 * scaleY), (int) (-10 * scaleX),
						(int) (-20 * scaleY));
				infoField.setTextColor(Color.YELLOW);
			} else if (isAuto) {
				infoField.setText("              AUTOPLAY. TAP SCREEN TO STOP");
				infoField.setTextSize(infoFieldTextSize1Str * scaleY);
				layoutParamsInfoField.setMargins((int) (10 * scaleX),
						(int) (20 * scaleY), (int) (-10 * scaleX),
						(int) (-20 * scaleY));
			} else
				infoField.setText("");
			Mode = REELS_ANIM;
		} else if (credit < bet[bet_index] * LINES[lines_index] && isAuto) {
			isAuto = false;
			tpanel.setAuto(false);
		}
	}

	private void takeWin() {
		reelStopHandler.removeCallbacks(ShowWins);
		credit += win;
		creditField.setText("" + credit);
		winField.setText("win paid \n" + win);
		winField.setTextColor(Color.YELLOW);
		winLineField.setText("");
		infoField.setText("       SLIDE THIS PANEL UP TO MAKE BET\n"
				+ "              OR TAP SCREEN TO PLAY");
		infoField.setTextColor(Color.WHITE);
		infoField.setTextSize(infoFieldTextSize2Str * scaleY);
		layoutParamsInfoField
				.setMargins((int) (60 * scaleX), (int) (12 * scaleY),
						(int) (-60 * scaleX), (int) (-12 * scaleY));
		btnStart.setText("START");
		btnAuto.setText("AUTO");
		btnBet.setText("BET");
		btnLines.setText("LINES");
		int[] LineFigNow = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		for (int i = 0; i < LINES[lines_index]; i++) {
			LineFigNow[i] = 1;
		}
		tpanel.setLines(LineFigNow);
		tpanel.setFGtablet(0);
		if (mp_winwait != null)
			mp_winwait.stop();
		mp_card.stop();
		Mode = SPIN_READY;
		dplayoutParams.setMargins((int) (640 * scaleX), 0,
				(int) (-640 * scaleX), 0);
		if (isAuto) {
			if (credit < bet[bet_index] * LINES[lines_index]) {
				isAuto = false;
				tpanel.setAuto(false);
			} else
				startReels();
		} else {
			MediaPlayer mp = MediaPlayer
					.create(getBaseContext(), R.raw.takewin);
			mp.start();
		}
	}

	private final Runnable StopReels = new Runnable() {
		public void run() {
			if (mp_spinloop != null) {
				try {
					//mp_spinloop.stop();
					mp_spinloop.release();
				} catch (IllegalStateException e) {
					mp_spinloop.release();
				}
			}
			if (!isAnimReels() && Mode == REELS_ANIM) {
				for (int i = 0; i < REELS; i++) {
					symbols[i] = reel[i].getSymbols();
				}
				winField.setText("");
				win = 0;
				CheckWin checkwin = new CheckWin();
				checkwin.checkLines(symbols, LINES[lines_index]);
				win = checkwin.win + SCATTER_TABLE[checkwin.scatter]
						* bet[bet_index] * LINES[lines_index];
				if (checkwin.winLines > 0) {

					int index = 0;
					showline = 0;
					showLines = new int[checkwin.winLines];
					showFigs = new int[checkwin.winLines];
					winPerLine = new int[checkwin.winLines];
					for (int i = 0; i < LINES[lines_index]; i++) {
						if (checkwin.winSimbols[i] > 0) {
							showLines[index] = i;
							showFigs[index] = checkwin.winSimbols[i] + 1;
							winPerLine[index] = checkwin.winPerSymbols[i];
							index++;
						}
					}
					isFGcountup = false;
					if (checkwin.scatter > 1) {
						showLines[index] = 0;
						showFigs[index] = 1;
						winPerLine[index] = SCATTER_TABLE[checkwin.scatter]
								* bet[bet_index] * LINES[lines_index];
						if (checkwin.scatter > 2)
							isFGcountup = true;
					}
					for (int i = 0; i < REELS; i++) {
						reel[i].playAnimSymbol(checkwin.scatter);
					}
				}
				Mode = SHOW_WIN;
				reelStopHandler.postDelayed(ShowWins, 20);
			} else {
				reelStopHandler.postDelayed(StopReels, 20);
			}
		}
	};

	private final Runnable ShowWins = new Runnable() {
		public void run() {
			if (win == 0) {
				if (isFG) {
					FGfork();
				} else {
					Mode = SPIN_READY;
					infoField
							.setText("       SLIDE THIS PANEL UP TO MAKE BET\n"
									+ "              OR TAP SCREEN TO PLAY");
					infoField.setTextColor(Color.WHITE);
					infoField.setTextSize(infoFieldTextSize2Str * scaleY);
					layoutParamsInfoField.setMargins((int) (60 * scaleX),
							(int) (12 * scaleY), (int) (-60 * scaleX),
							(int) (-12 * scaleY));
					if (isAuto) {
						if (credit < bet[bet_index] * LINES[lines_index]) {
							isAuto = false;
							tpanel.setAuto(false);
						} else
							startReels();
					}
				}
			} else if (Mode == SHOW_WIN || (Mode == WIN_WAIT && !isAuto)) {
				if (Mode == SHOW_WIN) {
					MediaPlayer mp = MediaPlayer.create(getBaseContext(),
							R.raw.win);
					mp.start();
					mp.setOnCompletionListener(new OnCompletionListener() {

						@Override
						public void onCompletion(MediaPlayer mp) {
							if (Mode == WIN_WAIT && !mp_winwait.isPlaying()
									&& !isAuto) {
								mp_winwait = MediaPlayer.create(
										getBaseContext(), R.raw.winwait);
								mp_winwait.setLooping(true);
								mp_winwait.start();
							}
						}
					});
				}
				int[] LineFigNow = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
				for (int i = 0; i < LINES[lines_index]; i++) {
					LineFigNow[i] = 1;
				}
				LineFigNow[showLines[showline]] = showFigs[showline];
				tpanel.setLines(LineFigNow);
				if (showFigs[showline] == 1)
					winLineField.setText("SCATTER " + winPerLine[showline]);
				else
					winLineField.setText("LINE " + winPerLine[showline]);
				showline++;
				if (!isFG)
					winField.setText("win " + win);
				else {
					fgwin += win;
					tpanel.setFGtop(true, fgwin);
				}
				winField.setTextColor(Color.WHITE);
				if (isFGcountup) {
					infoField.setText("                   BONUS FEATURE WON");
					infoField.setTextColor(Color.YELLOW);
					infoField.setTextSize(infoFieldTextSize1Str * scaleY);
					layoutParamsInfoField.setMargins((int) (10 * scaleX),
							(int) (20 * scaleY), (int) (-10 * scaleX),
							(int) (-20 * scaleY));
					MediaPlayer mp = MediaPlayer.create(getBaseContext(),
							R.raw.fgstart);
					mp.start();
					mp.setOnCompletionListener(new OnCompletionListener() {

						@Override
						public void onCompletion(MediaPlayer mp) {
							if (!isFG) {
								Mode = FG_READY;
								tpanel.setFGtablet(1);
								fgwin = win;
								winLineField.setText("");
								winField.setText("");
								tpanel.setFGtop(true, fgwin);
								infoField
										.setText("        TAP SCREEN TO START FREE GAMES");
								infoField.setTextColor(Color.YELLOW);
							}
						}
					});
					fggames += 15;
					Mode = DISABLE;
					if (isFG) {
						tpanel.setFGtablet(1);
						reelStopHandler.postDelayed(ShowWins, 6000);
					}
				} else {
					if ((Mode == SHOW_WIN || Mode == WIN_WAIT)
							&& showline == showLines.length) {
						if (Mode == SHOW_WIN && !isFG) {
							infoField
									.setText("YOU WIN. SLIDE THIS PANEL UP TO PLAY DOUBLE\n"
											+ "             OR TAP SCREEN TO TAKE WIN");
							infoField.setTextColor(Color.YELLOW);
							infoField.setTextSize(infoFieldTextSize2Str * scaleY);
							layoutParamsInfoField.setMargins(
									(int) (60 * scaleX), (int) (12 * scaleY),
									(int) (-60 * scaleX), (int) (-12 * scaleY));
							Mode = WIN_WAIT;
							// mp_spinloop =
							// MediaPlayer.create(getBaseContext(),
							// R.raw.reelloop);
							// mp_spinloop.setLooping(true);
						} else if (Mode == SHOW_WIN && isFG) {
							Mode = DISABLE;
						}
						showline = 0;

					}
					if (Mode == WIN_WAIT && !isAuto)
						reelStopHandler.postDelayed(ShowWins, 1000);
					else
						reelStopHandler.postDelayed(ShowWins, 700);
				}

			} else if (Mode == DISABLE && isFG) {
				FGfork();
			} else {
				takeWin();
			}
		}
	};

	private final Runnable ResetDouble = new Runnable() {
		public void run() {
			dpanel.setScreen(0, 0, 0);
			if (win > 0) {
				Mode = DOUBLE;
				mp_card = MediaPlayer.create(getBaseContext(), R.raw.card);
				mp_card.setLooping(true);
				mp_card.start();
				dpanel.setWon(win);
			} else {
				Mode = SPIN_READY;
				dplayoutParams.setMargins((int) (640 * scaleX), 0,
						(int) (-640 * scaleX), 0);
				dpanel.setLayoutParams(dplayoutParams);
				btnStart.setText("START");
				btnAuto.setText("AUTO");
				btnBet.setText("BET");
				btnLines.setText("LINES");
			}
		}
	};

	private void FGfork() {
		if (fgcount < fggames) {
			fgcount++;
			winLineField.setText("");
			tpanel.setFGtablet(0);
			Mode = SPIN_READY;
			startReels();
		} else {
			isFG = false;
			win = fgwin;
			fggames = 0;
			fgcount = 0;
			winField.setText("win " + win);
			mp_fgloop.stop();
			tpanel.setFGtablet(2);
			MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.fgend);
			mp.start();
			mp.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					tpanel.setFGtop(false, fgwin);
					if (isAuto)
						takeWin();
					else {
						Mode = WIN_WAIT;
						infoField
								.setText("YOU WIN. SLIDE THIS PANEL UP TO PLAY DOUBLE\n"
										+ "             OR TAP SCREEN TO TAKE WIN");
						infoField.setTextColor(Color.YELLOW);
						infoField.setTextSize(infoFieldTextSize2Str * scaleY);
						layoutParamsInfoField.setMargins((int) (60 * scaleX),
								(int) (12 * scaleY), (int) (-60 * scaleX),
								(int) (-12 * scaleY));
					}
				}
			});
		}
	}

}